package cn.dzhang.spring.boot.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import cn.dzhang.spring.boot.annotation.SecurityOperation;

public abstract class JpaAdapter<T, ID extends Serializable> implements
		JpaService<T, ID> {

	protected abstract JpaRepository<T, ID> getRepository();

	@SecurityOperation
	@Override
	public List<T> findAll() {
		return getRepository().findAll();
	}

	@Override
	public T findById(ID id) {
		return getRepository().findOne(id);
	}

	@SecurityOperation
	@Override
	public T saveOrUpdate(T entity) {
		return getRepository().save(entity);
	}

	@Override
	public List<T> saveOrUpdate(List<T> entities) {
		return getRepository().save(entities);
	}

	@Override
	public long count() {
		return getRepository().count();
	}

	@Override
	public boolean exits(ID id) {
		return getRepository().exists(id);
	}

	@Override
	public boolean deleteById(ID id) {
		getRepository().delete(id);
		return true;
	}

	@Override
	public boolean delete(T entity) {
		getRepository().delete(entity);
		return true;
	}

	@Override
	public boolean deleteAll() {
		getRepository().deleteAll();
		return true;
	}

	@Override
	public boolean deleteAll(List<T> entities) {
		getRepository().delete(entities);
		return true;
	}

}
